package com.hengjun.baccarat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hengjun.baccarat.common.ConfigData;
import com.hengjun.baccarat.net.Server;
import com.hengjun.baccarat.net.UpdateManager;
import com.hengjun.baccarat.net.UrlConn;
import com.hengjun.baccarat.pojo.GlobalService;
import com.hengjun.baccarat.utils.BcSysUser;
import com.hengjun.baccarat.utils.RecordCore;
import com.hengjun.baccarat.utils.ThreadManager;

public class Activity_Control extends Activity {
	private ListView listView;
	private TextView xianText, heText, zhuangText, xianduiText, zhuangduiText,
			benjinText, addMoneyText;
	private LinearLayout imageNumberTwo, imageNumberOne, imageNumberThree,
			imageNumberFour, imageNumberFive;
	private Button oneButton, twoButton, threeButton, fourButton, fiveButton,
			sixButton, sevenButton, eightButton, nineButton, zeroButton,
			dotButton, fallBackButton, cancelButton;
	private int hasSelect = 0;
	private Button zhuangRedButton, xianBlueButton, heOrangeButton,
			zhuangduiRedButton, xianduiBlueButton, resetAllButton,
			determinOpenButton;
	private TextView orderNumber, principal;
	private BettingListAdapter dapter;
	private double totalMoney;
	private String bettingResult = "";
	private String[] list = new String[] {};
	private List<RecordCore> rcList = GlobalService.list;
	private ProgressDialog dialog;
	private TextView rounds;
	private TextView totalMatch;

	private AutoUpLoadThead autoUpLoadThead;
	private Thread uploadThread;

	private static List<String> map = new ArrayList<String>();
	private static double xianBetting = 0;
	private static double heBetting = 0;
	private static double zhuangBetting = 0;
	private static double xianduiBetting = 0;
	private static double zhuangduiBetting = 0;

	private boolean zhuangRedSelected = false;
	private boolean heOrangeSelected = false;
	private boolean xianBlueSelected = false;
	private boolean zhuangduiRedSelected = false;
	private boolean xianduiBlueSelected = false;

	private double xianMoney = 0d;
	private double blankMoney = 0d;
	private double tieMoney = 0d;
	private double xianduiMoney = 0d;
	private double blankDoubleMoney = 0d;
	public static double captial = 0d;
	public static double addMoney = 0d;
	public static double totalAddMoney = 0d;
	public static Map<String, Object> tempmap = new HashMap<String, Object>();
	private boolean isExitt = false;
	private TotalMatchThread totalMatchThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_control);
		Server.SERVER.context = Activity_Control.this;

		ThreadManager.handler = new testHander();
		list = this.getIntent().getStringArrayExtra("list");
		captial = Double.parseDouble(list[2]);
		totalAddMoney = Double.parseDouble(list[4]);
		initView();

		autoUpLoadThead = new AutoUpLoadThead();
//		autoUpLoadThead.start();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		totalMatchThread = new TotalMatchThread();
		totalMatchThread.start();
		if (tempmap.get("captial") != null) {
			captial = Double.parseDouble(tempmap.get("captial").toString());
		}
		if (tempmap.get("totalAddMoney") != null) {
			totalAddMoney = Double.parseDouble(tempmap.get("totalAddMoney")
					.toString());
		}
		autoUpLoadThead.setCode("");
		imageNumberTwo.setBackgroundColor(Color.WHITE);
		imageNumberOne.setBackgroundColor(Color.WHITE);
		imageNumberThree.setBackgroundColor(Color.WHITE);
		imageNumberFour.setBackgroundColor(Color.WHITE);
		imageNumberFive.setBackgroundColor(Color.WHITE);
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.bettingList);
		dapter = new BettingListAdapter(this, map, R.layout.batting_item);
		listView.setAdapter(dapter);

		xianText = (TextView) findViewById(R.id.xainText);
		heText = (TextView) findViewById(R.id.heText);
		zhuangText = (TextView) findViewById(R.id.zhuangText);
		xianduiText = (TextView) findViewById(R.id.xianduiText);
		zhuangduiText = (TextView) findViewById(R.id.zhuangduiText);
		addMoneyText = (TextView) findViewById(R.id.addMoneyText);

		imageNumberTwo = (LinearLayout) findViewById(R.id.imagenumber_two);
		imageNumberOne = (LinearLayout) findViewById(R.id.imagenumber_one);
		imageNumberThree = (LinearLayout) findViewById(R.id.imagenumber_three);
		imageNumberFour = (LinearLayout) findViewById(R.id.imagenumber_four);
		imageNumberFive = (LinearLayout) findViewById(R.id.imagenumber_five);
		imageNumberTwo.setBackgroundColor(Color.WHITE);
		imageNumberOne.setBackgroundColor(Color.WHITE);
		imageNumberThree.setBackgroundColor(Color.WHITE);
		imageNumberFour.setBackgroundColor(Color.WHITE);
		imageNumberFive.setBackgroundColor(Color.WHITE);

		oneButton = (Button) findViewById(R.id.number_one);
		twoButton = (Button) findViewById(R.id.number_two);
		threeButton = (Button) findViewById(R.id.number_three);
		fourButton = (Button) findViewById(R.id.number_four);
		fiveButton = (Button) findViewById(R.id.number_five);
		sixButton = (Button) findViewById(R.id.number_six);
		sevenButton = (Button) findViewById(R.id.number_seven);
		eightButton = (Button) findViewById(R.id.number_eight);
		nineButton = (Button) findViewById(R.id.number_nine);
		zeroButton = (Button) findViewById(R.id.number_zero);
		dotButton = (Button) findViewById(R.id.number_dot);
		fallBackButton = (Button) findViewById(R.id.number_fallBack);
		cancelButton = (Button) findViewById(R.id.number_cancel);
		benjinText = (TextView) findViewById(R.id.benjinText);

		oneButton.setOnClickListener(new NumberButtonListener());
		twoButton.setOnClickListener(new NumberButtonListener());
		threeButton.setOnClickListener(new NumberButtonListener());
		fourButton.setOnClickListener(new NumberButtonListener());
		fiveButton.setOnClickListener(new NumberButtonListener());
		sixButton.setOnClickListener(new NumberButtonListener());
		sevenButton.setOnClickListener(new NumberButtonListener());
		eightButton.setOnClickListener(new NumberButtonListener());
		nineButton.setOnClickListener(new NumberButtonListener());
		zeroButton.setOnClickListener(new NumberButtonListener());
		dotButton.setOnClickListener(new NumberButtonListener());
		fallBackButton.setOnClickListener(new NumberButtonListener());
		cancelButton.setOnClickListener(new NumberButtonListener());

		zhuangRedButton = (Button) findViewById(R.id.zhuangRedButton);
		xianBlueButton = (Button) findViewById(R.id.xianBlueButton);
		heOrangeButton = (Button) findViewById(R.id.heOrangeButton);
		zhuangduiRedButton = (Button) findViewById(R.id.zhuangduiRedButton);
		xianduiBlueButton = (Button) findViewById(R.id.xianduiBlueButton);
		resetAllButton = (Button) findViewById(R.id.resetAllButton);
		determinOpenButton = (Button) findViewById(R.id.determinOpenButton);
		zhuangduiRedButton.setEnabled(false);
		xianduiBlueButton.setEnabled(false);

		zhuangRedButton.setOnClickListener(new OpenButtonListener());
		xianBlueButton.setOnClickListener(new OpenButtonListener());
		heOrangeButton.setOnClickListener(new OpenButtonListener());
		zhuangduiRedButton.setOnClickListener(new OpenButtonListener());
		xianduiBlueButton.setOnClickListener(new OpenButtonListener());
		resetAllButton.setOnClickListener(new OpenButtonListener());
		determinOpenButton.setOnClickListener(new OpenButtonListener());

		orderNumber = (TextView) findViewById(R.id.orderNumber);
		orderNumber.setText(rcList.get(OrderList.point).getRecordsn());
		addMoneyText.setText(GlobalService.getDoubleString(totalAddMoney));
		rounds = (TextView) findViewById(R.id.rounds);
		rounds.setText("0");
		principal = (TextView) findViewById(R.id.principal);
		totalMatch = (TextView) findViewById(R.id.totalmathch);

		if (captial < 0) {
			principal.setTextColor(Color.RED);
			principal.setText(String.valueOf(captial));
		} else {
			principal.setTextColor(Color.BLACK);
			principal.setText(String.valueOf(captial));
		}
		if (Double.parseDouble(list[3]) < 0) {
			benjinText.setTextColor(Color.RED);
			benjinText.setText(list[3]);
		} else {
			benjinText.setTextColor(Color.BLACK);
			benjinText.setText(list[3]);
		}
	}
private String matchNum = "";
	private class TotalMatchThread extends Thread {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				matchNum = UrlConn.getTotalMatch(list[1]);
				handler.obtainMessage(12).sendToTarget();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				handler.obtainMessage(11).sendToTarget();
			}
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (captial < 0) {
			principal.setTextColor(Color.RED);
			principal.setText(GlobalService.getDoubleString(captial));
		} else {
			principal.setTextColor(Color.BLACK);
			principal.setText(GlobalService.getDoubleString(captial));
		}
		addMoneyText.setText(GlobalService.getDoubleString(totalAddMoney));

		imageNumberTwo.setBackgroundColor(Color.WHITE);
		imageNumberOne.setBackgroundColor(Color.WHITE);
		imageNumberThree.setBackgroundColor(Color.WHITE);
		imageNumberFour.setBackgroundColor(Color.WHITE);
		imageNumberFive.setBackgroundColor(Color.WHITE);
		rounds.setText(String.valueOf(GlobalService.totalMatch()));
	}

	private class BettingListAdapter extends BaseAdapter implements
			OnClickListener {
		private List<String> map;
		private int resource;
		private LayoutInflater inflater;
		private TextView showType;
		private TextView showMoney;
		private Button deleteItem;
		private Context context;
		private int positionItem;

		public BettingListAdapter(Context context, List<String> map,
				int resource) {
			this.map = map;
			this.resource = resource;
			this.context = context;
		}

		@Override
		public int getCount() {
			return map.size();
		}

		@Override
		public Object getItem(int position) {
			return map.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			positionItem = position;
			if (convertView == null) {
				inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(resource, null);
				showType = (TextView) convertView.findViewById(R.id.showType);
				showMoney = (TextView) convertView.findViewById(R.id.showMoney);
				deleteItem = (Button) convertView.findViewById(R.id.deleteItem);
				deleteItem.setOnClickListener(this);
			}
			if (position < map.size() && map.size() > 0) {
				if (map.get(position) != null) {
					String[] str = map.get(position).split("[:]");
					if (str[0].equals("0") && map.get(position) != null) {
						showType.setText("閑");
						showMoney.setText(str[1].toString());
					} else if (str[0].equals("1") && map.get(position) != null) {
						showType.setText("和");
						showMoney.setText(str[1].toString());
					} else if (str[0].equals("2") && map.get(position) != null) {
						showType.setText("莊");
						showMoney.setText(str[1].toString());
					} else if (str[0].equals("3") && map.get(position) != null) {
						showType.setText("閑對");
						showMoney.setText(str[1].toString());
					} else if (str[0].equals("4") && map.get(position) != null) {
						showType.setText("莊對");
						showMoney.setText(str[1].toString());
					}
				}
			}
			return convertView;
		}

		@Override
		public void onClick(View v) {
			map.remove(positionItem);
			dapter.notifyDataSetChanged();
		}
	}

	private class OpenButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			double winTotal = 0d;
			String openType = null;

			switch (v.getId()) {
			case R.id.zhuangRedButton:
				if (zhuangRedSelected == false) {
					zhuangRedButton.setSelected(true);
					heOrangeButton.setEnabled(false);
					xianBlueButton.setEnabled(false);
					zhuangduiRedButton.setEnabled(true);
					xianduiBlueButton.setEnabled(true);
					zhuangRedSelected = true;
				} else {
					zhuangRedButton.setSelected(false);
					heOrangeButton.setEnabled(true);
					xianBlueButton.setEnabled(true);
					xianduiBlueButton.setEnabled(false);
					zhuangduiRedButton.setEnabled(false);
					xianduiBlueButton.setEnabled(false);
					zhuangRedSelected = false;
					if (xianduiBlueButton.isSelected() == true) {
						xianduiBlueButton.setSelected(false);
						xianduiBlueSelected = false;
					}
					if (zhuangduiRedButton.isSelected() == true) {
						zhuangduiRedButton.setSelected(false);
						zhuangduiRedSelected = false;
					}
				}
				break;
			case R.id.xianBlueButton:
				if (xianBlueSelected == false) {
					xianBlueButton.setSelected(true);
					zhuangRedButton.setEnabled(false);
					heOrangeButton.setEnabled(false);
					zhuangduiRedButton.setEnabled(true);
					xianduiBlueButton.setEnabled(true);
					xianBlueSelected = true;
				} else {
					xianBlueButton.setSelected(false);
					zhuangRedButton.setEnabled(true);
					heOrangeButton.setEnabled(true);
					zhuangduiRedButton.setEnabled(false);
					xianduiBlueButton.setEnabled(false);
					xianBlueSelected = false;
					if (xianduiBlueButton.isSelected() == true) {
						xianduiBlueButton.setSelected(false);
						xianduiBlueSelected = false;
					}
					if (zhuangduiRedButton.isSelected() == true) {
						zhuangduiRedButton.setSelected(false);
						zhuangduiRedSelected = false;
					}
				}
				break;
			case R.id.heOrangeButton:
				if (heOrangeSelected == false) {
					heOrangeButton.setSelected(true);
					zhuangRedButton.setEnabled(false);
					xianBlueButton.setEnabled(false);
					zhuangduiRedButton.setEnabled(true);
					xianduiBlueButton.setEnabled(true);
					heOrangeSelected = true;
				} else {
					heOrangeButton.setSelected(false);
					zhuangRedButton.setEnabled(true);
					xianBlueButton.setEnabled(true);
					zhuangduiRedButton.setEnabled(false);
					xianduiBlueButton.setEnabled(false);
					heOrangeSelected = false;
					if (xianduiBlueButton.isSelected() == true) {
						xianduiBlueButton.setSelected(false);
						xianduiBlueSelected = false;
					}
					if (zhuangduiRedButton.isSelected() == true) {
						zhuangduiRedButton.setSelected(false);
						zhuangduiRedSelected = false;
					}
				}
				break;

			case R.id.zhuangduiRedButton:
				if (zhuangduiRedSelected == false) {
					zhuangduiRedButton.setSelected(true);
					zhuangduiRedSelected = true;
				} else {
					zhuangduiRedButton.setSelected(false);
					zhuangduiRedSelected = false;
				}
				break;
			case R.id.xianduiBlueButton:
				if (xianduiBlueSelected == false) {
					xianduiBlueButton.setSelected(true);
					xianduiBlueSelected = true;
				} else {
					xianduiBlueButton.setSelected(false);
					xianduiBlueSelected = false;
				}
				break;
			case R.id.resetAllButton:
				resetButton("");
				break;
			case R.id.determinOpenButton:
				// 莊
				if (zhuangRedButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == false) {
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.BANK_STYLE_NAME));
					openType = ConfigData.BANK_STYLE;
					GlobalService.culBigStreetCellObject(
							ConfigData.BANK_STYLE_NAME, winTotal);
				}
				// 莊,莊對
				else if (zhuangRedButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == false) {
					openType = ConfigData.BANK_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.BANK_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.BANK_DOUBLE_STYLE_NAME, winTotal);

				}
				// 莊,閑對
				else if (zhuangRedButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.BANK_PLAYER_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.BANK_PLAYER_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.BANK_PLAYER_DOUBLE_STYLE_NAME, winTotal);
				}
				// 莊,莊對,閑對
				else if (zhuangRedButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.BANK_TWO_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.BANK_TWO_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.BANK_TWO_DOUBLE_STYLE_NAME, winTotal);
				}
				// 閑
				else if (xianBlueButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == false) {
					openType = ConfigData.PLAYERS_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.PLAYERS_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.PLAYERS_STYLE_NAME, winTotal);
				}
				// 閑,莊對
				else if (xianBlueButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == false) {
					openType = ConfigData.PLAYERS_BANK_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.PLAYERS_BANK_DOUBLE_STYLE_NAME));
					GlobalService
							.culBigStreetCellObject(
									ConfigData.PLAYERS_BANK_DOUBLE_STYLE_NAME,
									winTotal);
				}
				// 閑,閑對
				else if (xianBlueButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.PLAYERS_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.PLAYERS_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.PLAYERS_DOUBLE_STYLE_NAME, winTotal);
				}
				// 閑,莊對,閑對
				else if (xianBlueButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.PLAYERS_TWO_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.PLAYERS_TOW_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.PLAYERS_TOW_STYLE_NAME, winTotal);
				}
				// 和
				else if (heOrangeButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == false) {
					openType = ConfigData.TIE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.TIE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.TIE_STYLE_NAME, winTotal);
				}
				// 和,莊對
				else if (heOrangeButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == false) {
					openType = ConfigData.TIE_BANK_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.TIE_BANK_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.TIE_BANK_DOUBLE_STYLE_NAME, winTotal);
				}
				// 和,閑對
				else if (heOrangeButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == false
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.TIE_PLAYER_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.TIE_PLAYER_DOUBLE_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.TIE_PLAYER_DOUBLE_STYLE_NAME, winTotal);
				}
				// 和,莊對,閑對
				else if (heOrangeButton.isSelected() == true
						&& zhuangduiRedButton.isSelected() == true
						&& xianduiBlueButton.isSelected() == true) {
					openType = ConfigData.TIE_TWO_DOUBLE_STYLE;
					winTotal = culWin(GlobalService
							.getOpenType(ConfigData.TIE_TOW_STYLE_NAME));
					GlobalService.culBigStreetCellObject(
							ConfigData.TIE_TOW_STYLE_NAME, winTotal);
				} else {
					break;
				}
				totalMoney = winTotal
						+ Double.parseDouble(principal.getText().toString()
								.equals("") ? "0" : principal.getText()
								.toString());

				captial = totalMoney;
				tempmap.put("captial", captial);
				bettingResult = "{\"match\":" + GlobalService.totalMatch()
						+ ",\"yaZd\":" + String.valueOf(blankDoubleMoney)
						+ ",\"yaZ\":" + String.valueOf(blankMoney)
						+ ",\"yaH\":" + String.valueOf(tieMoney) + ",\"yaX\":"
						+ String.valueOf(xianMoney) + ",\"yaXd\":"
						+ String.valueOf(xianduiMoney) + ",\"result\":\""
						+ openType + "\",\"resultWin\":" + winTotal
						+ ",\"total\":" + totalMoney
						+ ",\"bacCartSysuerIndex\":"
						+ BcSysUser.getbcSysUser().getUserIndex()
						+ ",\"recordindex\":" + list[1] + ",\"addMoney\":"
						+ addMoney + "}";
				principal.setText(GlobalService.getDoubleString(totalMoney));
				GlobalService.addBettingResultList(bettingResult);
				addMoney = 0d;
				resetButton("determinOpenButton");
				rounds.setText(String.valueOf(GlobalService.totalMatch()));
				break;
			}
		}
	}

	private double culWin(String num) {
		double blankdoubleBenifit = 0d;// 莊對盈利
		double blankBenifit = 0d;// 莊盈利
		double tieBenifit = 0d;// 閑盈利
		double playerBenifit = 0d;// 閑盈利
		double playerDoubleBenifit = 0d;// 閑對盈利

		for (int i = 0; i < map.size(); i++) {
			String[] str = map.get(i).split("[:]");
			// 閑
			if (str[0].equals("0") && map.get(i) != null) {
				xianMoney = Double.parseDouble(str[1].toString());
			}
			// 和
			if (str[0].equals("1") && map.get(i) != null) {
				tieMoney = Double.parseDouble(str[1].toString());
			}
			// 莊
			if (str[0].equals("2") && map.get(i) != null) {
				blankMoney = Double.parseDouble(str[1].toString());
			}
			// 閑對
			if (str[0].equals("3") && map.get(i) != null) {
				xianduiMoney = Double.parseDouble(str[1].toString());
			}
			// 莊對
			if (str[0].equals("4") && map.get(i) != null) {
				blankDoubleMoney = Double.parseDouble(str[1].toString());
			}
		}

		blankdoubleBenifit = -blankDoubleMoney;
		playerDoubleBenifit = -xianduiMoney;
		if (num.equals(ConfigData.BANK_DOUBLE_STYLE)
				|| num.equals(ConfigData.BANK_TWO_DOUBLE_STYLE)
				|| num.equals(ConfigData.PLAYERS_BANK_DOUBLE_STYLE)
				|| num.equals(ConfigData.PLAYERS_TWO_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_BANK_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_TWO_DOUBLE_STYLE)

		) {
			blankdoubleBenifit = blankDoubleMoney * ConfigData.BANK_DOUBLE_ODDS;
		}
		if (num.equals(ConfigData.BANK_PLAYER_DOUBLE_STYLE)
				|| num.equals(ConfigData.BANK_TWO_DOUBLE_STYLE)
				|| num.equals(ConfigData.PLAYERS_DOUBLE_STYLE)
				|| num.equals(ConfigData.PLAYERS_TWO_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_PLAYER_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_TWO_DOUBLE_STYLE)

		) {
			playerDoubleBenifit = xianduiMoney * ConfigData.PLAYER_DOUBLE_ODDS;
		}

		if (!num.equals(ConfigData.TIE_STYLE)
				&& !num.equals(ConfigData.TIE_PLAYER_DOUBLE_STYLE)
				&& !num.equals(ConfigData.TIE_BANK_DOUBLE_STYLE)
				&& !num.equals(ConfigData.TIE_TWO_DOUBLE_STYLE)

		) {

			if (num.equals(ConfigData.BANK_STYLE)
					|| num.equals(ConfigData.BANK_PLAYER_DOUBLE_STYLE)
					|| num.equals(ConfigData.BANK_DOUBLE_STYLE)
					|| num.equals(ConfigData.BANK_TWO_DOUBLE_STYLE)) {

				blankBenifit = blankMoney * ConfigData.BANK_ODDS;

			} else {
				blankBenifit = blankMoney * -1;
			}
			if (num.equals(ConfigData.PLAYERS_STYLE)
					|| num.equals(ConfigData.PLAYERS_BANK_DOUBLE_STYLE)
					|| num.equals(ConfigData.PLAYERS_DOUBLE_STYLE)
					|| num.equals(ConfigData.PLAYERS_TWO_DOUBLE_STYLE)) {

				playerBenifit = xianMoney * ConfigData.PLAYER_ODDS;

			} else {
				playerBenifit = xianMoney * -1;
			}

		}
		if (num.equals(ConfigData.TIE_STYLE)
				|| num.equals(ConfigData.TIE_PLAYER_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_BANK_DOUBLE_STYLE)
				|| num.equals(ConfigData.TIE_TWO_DOUBLE_STYLE)

		) {
			tieBenifit = tieMoney * ConfigData.TIEO_DDS;
		} else {
			tieBenifit = tieMoney * -1;
		}
		return blankdoubleBenifit + blankBenifit + tieBenifit + playerBenifit
				+ playerDoubleBenifit;
	}

	private void resetButton(String type) {
		if (type.equals("determinOpenButton")) {
			xianMoney = 0d;
			xianduiMoney = 0d;
			blankMoney = 0d;
			blankDoubleMoney = 0d;
			tieMoney = 0d;

			map.clear();
			dapter = new BettingListAdapter(this, map, R.layout.batting_item);
			listView.setAdapter(dapter);
		}
		zhuangRedSelected = false;
		heOrangeSelected = false;
		xianBlueSelected = false;
		zhuangduiRedSelected = false;
		xianduiBlueSelected = false;

		zhuangRedButton.setSelected(false);
		xianBlueButton.setSelected(false);
		heOrangeButton.setSelected(false);
		zhuangduiRedButton.setSelected(false);
		xianduiBlueButton.setSelected(false);

		zhuangRedButton.setEnabled(true);
		xianBlueButton.setEnabled(true);
		heOrangeButton.setEnabled(true);
		zhuangduiRedButton.setEnabled(false);
		xianduiBlueButton.setEnabled(false);
	}

	public void buttonListener(View v) {

		if (v.getId() == R.id.goToMap) {
			if (!isAutoUploadFlag) {
				autoUpLoadThead.setCode("-1");
				Intent intent = new Intent(Activity_Control.this,
						Activity_Map.class);
				intent.putExtra("bettingResult", bettingResult);
				intent.putExtra("addMoney", addMoney);
				intent.putExtra("list", list);
				startActivity(intent);
			} else {
				Toast.makeText(Activity_Control.this, "自動上傳中，請稍後...",
						Toast.LENGTH_SHORT).show();
			}

		} else if (v.getId() == R.id.retrunToList) {
			if (checkAuto) {
				try {
					Activity_Control.addMoney = 0d;
					Activity_Map.addMoney = 0d;
					tempmap.remove("captial");
					tempmap.remove("addMoney");
					tempmap.remove("totalAddMoney");
					isExitt = true;
					autoUpLoadThead.setCode("-1");
//					autoUpLoadThead.interrupt();
					uploadThread = new Thread(run);
					uploadThread.start();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(Activity_Control.this, "程序發生錯誤退出!",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				handler.obtainMessage(9).sendToTarget();
			}

		} else if (v.getId() == R.id.imagenumber_one) {
			if (hasSelect != R.id.imagenumber_one) {
				imageNumberTwo.setBackgroundColor(Color.WHITE);
				imageNumberOne.setBackgroundColor(Color.YELLOW);
				imageNumberThree.setBackgroundColor(Color.WHITE);
				imageNumberFour.setBackgroundColor(Color.WHITE);
				imageNumberFive.setBackgroundColor(Color.WHITE);
				hasSelect = R.id.imagenumber_one;
			} else {
				imageNumberOne.setBackgroundColor(Color.WHITE);
				hasSelect = 0;
			}
		} else if (v.getId() == R.id.imagenumber_two) {
			if (hasSelect != R.id.imagenumber_two) {
				imageNumberTwo.setBackgroundColor(Color.YELLOW);
				imageNumberOne.setBackgroundColor(Color.WHITE);
				imageNumberThree.setBackgroundColor(Color.WHITE);
				imageNumberFour.setBackgroundColor(Color.WHITE);
				imageNumberFive.setBackgroundColor(Color.WHITE);
				hasSelect = R.id.imagenumber_two;
			} else {
				imageNumberTwo.setBackgroundColor(Color.WHITE);
				hasSelect = 0;
			}
		} else if (v.getId() == R.id.imagenumber_three) {
			if (hasSelect != R.id.imagenumber_three) {
				imageNumberTwo.setBackgroundColor(Color.WHITE);
				imageNumberOne.setBackgroundColor(Color.WHITE);
				imageNumberThree.setBackgroundColor(Color.YELLOW);
				imageNumberFour.setBackgroundColor(Color.WHITE);
				imageNumberFive.setBackgroundColor(Color.WHITE);
				hasSelect = R.id.imagenumber_three;
			} else {
				imageNumberThree.setBackgroundColor(Color.WHITE);
				hasSelect = 0;
			}
		} else if (v.getId() == R.id.imagenumber_four) {
			if (hasSelect != R.id.imagenumber_four) {
				imageNumberTwo.setBackgroundColor(Color.WHITE);
				imageNumberOne.setBackgroundColor(Color.WHITE);
				imageNumberThree.setBackgroundColor(Color.WHITE);
				imageNumberFour.setBackgroundColor(Color.YELLOW);
				imageNumberFive.setBackgroundColor(Color.WHITE);
				hasSelect = R.id.imagenumber_four;
			} else {
				imageNumberFour.setBackgroundColor(Color.WHITE);
				hasSelect = 0;
			}
		} else if (v.getId() == R.id.imagenumber_five) {
			if (hasSelect != R.id.imagenumber_five) {
				imageNumberTwo.setBackgroundColor(Color.WHITE);
				imageNumberOne.setBackgroundColor(Color.WHITE);
				imageNumberThree.setBackgroundColor(Color.WHITE);
				imageNumberFour.setBackgroundColor(Color.WHITE);
				imageNumberFive.setBackgroundColor(Color.YELLOW);
				hasSelect = R.id.imagenumber_five;
			} else {
				imageNumberFive.setBackgroundColor(Color.WHITE);
				hasSelect = 0;
			}
		}
	}

	public void determine(View v) {

		if (!xianText.getText().toString().equals("")) {
			if (xianText
					.getText()
					.toString()
					.substring(xianText.getText().toString().length() - 1,
							xianText.getText().toString().length()).equals(".")) {
				xianText.setText(xianText.getText().toString() + "0");
			}
			xianBetting = Double.parseDouble(GlobalService
					.getDoubleString(Double.parseDouble(xianText.getText()
							.toString())));
			String str = "0" + ":" + String.valueOf(xianBetting);
			boolean flag = true;
			for (int i = 0; i < map.size(); i++) {
				String s = map.get(i);
				String[] strArray = s.split("[:]");
				if (strArray[0].equals("0")) {
					double totalMoney = Double.parseDouble(strArray[1])
							+ xianBetting;
					strArray[1] = String.valueOf(totalMoney);
					s = strArray[0] + ":" + strArray[1];
					map.set(i, s);
					flag = false;
				}
			}
			if (flag) {
				map.add(str);
			}
		}
		if (!heText.getText().toString().equals("")) {
			if (heText
					.getText()
					.toString()
					.substring(heText.getText().toString().length() - 1,
							heText.getText().toString().length()).equals(".")) {
				heText.setText(heText.getText().toString() + "0");
			}
			heBetting = Double.parseDouble(GlobalService.getDoubleString(Double
					.parseDouble(heText.getText().toString())));
			String str = "1" + ":" + String.valueOf(heBetting);
			boolean flag = true;
			for (int i = 0; i < map.size(); i++) {
				String s = map.get(i);
				String[] strArray = s.split("[:]");
				if (strArray[0].equals("1")) {
					double totalMoney = Double.parseDouble(strArray[1])
							+ heBetting;
					strArray[1] = String.valueOf(totalMoney);
					s = strArray[0] + ":" + strArray[1];
					map.set(i, s);
					flag = false;
				}
			}
			if (flag) {
				map.add(str);
			}
		}
		if (!zhuangText.getText().toString().equals("")) {
			if (zhuangText
					.getText()
					.toString()
					.substring(zhuangText.getText().toString().length() - 1,
							zhuangText.getText().toString().length())
					.equals(".")) {
				zhuangText.setText(zhuangText.getText().toString() + "0");
			}
			zhuangBetting = Double.parseDouble(GlobalService
					.getDoubleString(Double.parseDouble(zhuangText.getText()
							.toString())));
			String str = "2" + ":" + String.valueOf(zhuangBetting);
			boolean flag = true;
			for (int i = 0; i < map.size(); i++) {
				String s = map.get(i);
				String[] strArray = s.split("[:]");
				if (strArray[0].equals("2")) {
					double totalMoney = Double.parseDouble(strArray[1])
							+ zhuangBetting;
					strArray[1] = String.valueOf(totalMoney);
					s = strArray[0] + ":" + strArray[1];
					map.set(i, s);
					flag = false;
				}
			}
			if (flag) {
				map.add(str);
			}
		}
		if (!xianduiText.getText().toString().equals("")) {
			if (xianduiText
					.getText()
					.toString()
					.substring(xianduiText.getText().toString().length() - 1,
							xianduiText.getText().toString().length())
					.equals(".")) {
				xianduiText.setText(xianduiText.getText().toString() + "0");
			}
			xianduiBetting = Double.parseDouble(GlobalService
					.getDoubleString(Double.parseDouble(xianduiText.getText()
							.toString())));
			String str = "3" + ":" + String.valueOf(xianduiBetting);
			boolean flag = true;
			for (int i = 0; i < map.size(); i++) {
				String s = map.get(i);
				String[] strArray = s.split("[:]");
				if (strArray[0].equals("3")) {
					double totalMoney = Double.parseDouble(strArray[1])
							+ xianduiBetting;
					strArray[1] = String.valueOf(totalMoney);
					s = strArray[0] + ":" + strArray[1];
					map.set(i, s);
					flag = false;
				}
			}
			if (flag) {
				map.add(str);
			}
		}
		if (!zhuangduiText.getText().toString().equals("")) {
			if (zhuangduiText
					.getText()
					.toString()
					.substring(zhuangduiText.getText().toString().length() - 1,
							zhuangduiText.getText().toString().length())
					.equals(".")) {
				zhuangduiText.setText(zhuangduiText.getText().toString() + "0");
			}
			zhuangduiBetting = Double.parseDouble(GlobalService
					.getDoubleString(Double.parseDouble(zhuangduiText.getText()
							.toString())));
			String str = "4" + ":" + String.valueOf(zhuangduiBetting);
			boolean flag = true;
			for (int i = 0; i < map.size(); i++) {
				String s = map.get(i);
				String[] strArray = s.split("[:]");
				if (strArray[0].equals("4")) {
					double totalMoney = Double.parseDouble(strArray[1])
							+ zhuangduiBetting;
					strArray[1] = String.valueOf(totalMoney);
					s = strArray[0] + ":" + strArray[1];
					map.set(i, s);
					flag = false;
				}
			}
			if (flag) {
				map.add(str);
			}
		}
		dapter = new BettingListAdapter(this, map, R.layout.batting_item);
		listView.setAdapter(dapter);

		xianText.setText("");
		heText.setText("");
		zhuangText.setText("");
		xianduiText.setText("");
		zhuangduiText.setText("");
	}

	private class NumberButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.number_one:
				setBettingText(oneButton.getText().toString());
				break;
			case R.id.number_two:
				setBettingText(twoButton.getText().toString());
				break;
			case R.id.number_three:
				setBettingText(threeButton.getText().toString());
				break;
			case R.id.number_four:
				setBettingText(fourButton.getText().toString());
				break;
			case R.id.number_five:
				setBettingText(fiveButton.getText().toString());
				break;
			case R.id.number_six:
				setBettingText(sixButton.getText().toString());
				break;
			case R.id.number_seven:
				setBettingText(sevenButton.getText().toString());
				break;
			case R.id.number_eight:
				setBettingText(eightButton.getText().toString());
				break;
			case R.id.number_nine:
				setBettingText(nineButton.getText().toString());
				break;
			case R.id.number_dot:
				setBettingText(".");
				break;
			case R.id.number_zero:
				setBettingText(zeroButton.getText().toString());
				break;
			case R.id.number_fallBack:
				setBettingText("fallBack");
				break;
			case R.id.number_cancel:
				setBettingText("");
				break;
			}
		}

		private void setBettingText(String num) {
			if (hasSelect == R.id.imagenumber_one) {
				if (num.equals("")) {
					xianText.setText("");
				} else if (num.equals("fallBack")) {
					if (!xianText.getText().equals("")) {
						if (xianText.getText().equals("0.")) {
							xianText.setText("");
						} else {
							xianText.setText(xianText
									.getText()
									.toString()
									.substring(
											0,
											xianText.getText().toString()
													.length() - 1));
						}
					}
				} else if (num.equals(".")) {
					if (xianText.getText().toString().equals("")) {
						xianText.setText(xianText.getText() + "0.");
					} else if (!xianText.getText().toString().contains(".")) {
						xianText.setText(xianText.getText() + ".");
					}
				} else {
					if (num.equals("0")
							&& xianText.getText().toString().equals("")) {

					} else {
						xianText.setText(xianText.getText() + num);
					}
				}
			} else if (hasSelect == R.id.imagenumber_two) {
				if (num.equals("")) {
					heText.setText("");
				} else if (num.equals("fallBack")) {
					if (!heText.getText().equals("")) {
						if (heText.getText().equals("0.")) {
							heText.setText("");
						} else {
							heText.setText(heText
									.getText()
									.toString()
									.substring(
											0,
											heText.getText().toString()
													.length() - 1));
						}
					}

				} else if (num.equals(".")) {
					if (heText.getText().toString().equals("")) {
						heText.setText(heText.getText() + "0.");
					} else if (!heText.getText().toString().contains(".")) {
						heText.setText(heText.getText() + ".");
					}
				} else {
					if (num.equals("0")
							&& heText.getText().toString().equals("")) {

					} else {
						heText.setText(heText.getText() + num);
					}
				}
			} else if (hasSelect == R.id.imagenumber_three) {
				if (num.equals("")) {
					zhuangText.setText("");
				} else if (num.equals("fallBack")) {
					if (!zhuangText.getText().equals("")) {
						if (zhuangText.getText().equals("0.")) {
							zhuangText.setText("");
						} else {
							zhuangText.setText(zhuangText
									.getText()
									.toString()
									.substring(
											0,
											zhuangText.getText().toString()
													.length() - 1));
						}
					}
				} else if (num.equals(".")) {
					if (zhuangText.getText().toString().equals("")) {
						zhuangText.setText(zhuangText.getText() + "0.");
					} else if (!zhuangText.getText().toString().contains(".")) {
						zhuangText.setText(zhuangText.getText() + ".");
					}
				} else {
					if (num.equals("0")
							&& zhuangText.getText().toString().equals("")) {

					} else {
						zhuangText.setText(zhuangText.getText() + num);
					}
				}
			} else if (hasSelect == R.id.imagenumber_four) {
				if (num.equals("")) {
					xianduiText.setText("");
				} else if (num.equals("fallBack")) {
					if (!xianduiText.getText().equals("")) {
						if (xianduiText.getText().equals("0.")) {
							xianduiText.setText("");
						} else {
							xianduiText.setText(xianduiText
									.getText()
									.toString()
									.substring(
											0,
											xianduiText.getText().toString()
													.length() - 1));
						}
					}
				} else if (num.equals(".")) {
					if (xianduiText.getText().toString().equals("")) {
						xianduiText.setText(xianduiText.getText() + "0.");
					} else if (!xianduiText.getText().toString().contains(".")) {
						xianduiText.setText(xianduiText.getText() + ".");
					}
				} else {
					if (num.equals("0")
							&& xianduiText.getText().toString().equals("")) {

					} else {
						xianduiText.setText(xianduiText.getText() + num);
					}
				}

			} else if (hasSelect == R.id.imagenumber_five) {
				if (num.equals("")) {
					zhuangduiText.setText("");
				} else if (num.equals("fallBack")) {
					if (!zhuangduiText.getText().equals("")) {
						if (zhuangduiText.getText().equals("0.")) {
							zhuangduiText.setText("");
						} else {
							zhuangduiText.setText(zhuangduiText
									.getText()
									.toString()
									.substring(
											0,
											zhuangduiText.getText().toString()
													.length() - 1));
						}
					}
				} else if (num.equals(".")) {
					if (zhuangduiText.getText().toString().equals("")) {
						zhuangduiText.setText(zhuangduiText.getText() + "0.");
					} else if (!zhuangduiText.getText().toString()
							.contains(".")) {
						zhuangduiText.setText(zhuangduiText.getText() + ".");
					}
				} else {
					if (num.equals("0")
							&& zhuangduiText.getText().toString().equals("")) {

					} else {
						zhuangduiText.setText(zhuangduiText.getText() + num);
					}
				}
			}
		}

	}

	private boolean isAutoUploadFlag = false;

	private class AutoUpLoadThead extends Thread {
		boolean flag = false;
		private String code = "";
		private boolean isRun = true;

		@Override
		public void run() {
			while (isRun) {
				try {
					String code = getCode();
					if (code.equals("")) {

						isAutoUploadFlag = true;
						handler.obtainMessage(7).sendToTarget();
						uploadData();
						isAutoUploadFlag = false;
						handler.obtainMessage(8).sendToTarget();
						Thread.sleep(60000 * 3);
					}
				} catch (Exception e) {
					e.printStackTrace();
					handler.obtainMessage(8).sendToTarget();
					isRun = false;
					break;
				}
			}
		}

		public synchronized String getCode() {
			while (flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
					Toast.makeText(Activity_Control.this, "程序發生錯誤退出",
							Toast.LENGTH_SHORT).show();
				}
			}
			return code;
		}

		public synchronized void setCode(String code) {

			if (code.equals("")) {
				flag = false;
				isRun = true;
				this.notify();
				this.code = code;
			} else {
				isRun = false;
				flag = true;
			}

		}
	}

	private synchronized void uploadData() {
		try {
			String result = UrlConn.queryPort(list[1]);
			if (result != null && !result.equals("-1")) {
				Thread.sleep(5000);
				GlobalService.upLoad(list[1], bettingResult);
			} else {
				GlobalService.list = UrlConn.RecordConn();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private boolean checkAuto = true;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent eve) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (checkAuto) {
				try {
					Activity_Control.addMoney = 0d;
					Activity_Map.addMoney = 0d;
					tempmap.remove("captial");
					tempmap.remove("addMoney");
					tempmap.remove("totalAddMoney");
					isExitt = true;
//					autoUpLoadThead.interrupt();
					autoUpLoadThead.setCode("-1");

					uploadThread = new Thread(run);
					uploadThread.start();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(Activity_Control.this, "程序發生錯誤退出!",
							Toast.LENGTH_SHORT).show();
				}
				return true;
			} else {
				handler.obtainMessage(9).sendToTarget();
			}
		}
		return false;
	}

	private Dialog netUploadError = null;
	private Runnable run = new Runnable() {

		@Override
		public void run() {

			// TODO Auto-generated method stub
			try {
				handler.obtainMessage(7).sendToTarget();
				handler.obtainMessage(1).sendToTarget();
				uploadData();
				handler.obtainMessage(8).sendToTarget();
				handler.obtainMessage(2).sendToTarget();
			} catch (Exception e) {
				handler.obtainMessage(8).sendToTarget();
				handler.obtainMessage(3).sendToTarget();
				e.printStackTrace();
			}
			autoUpLoadThead.setCode("");
			if (isExitt) {
				handler.obtainMessage(6).sendToTarget();
			}
		}

	};
	private Dialog netErrorDialog = null;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 1) {
				try {
					dialog = Login.createProgressDialog(Activity_Control.this,
							"上傳數據");
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			if (msg.what == 2) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			if (msg.what == 3) {
				netUploadError = new Dialog(Activity_Control.this,
						R.style.MyDialogStyle);
				netUploadError.setContentView(R.layout.erro_dialog);
				TextView message = (TextView) netUploadError
						.findViewById(R.id.textView1);
				message.setText("網絡故障,上傳失敗!");
				Button exit = (Button) netUploadError
						.findViewById(R.id.button1);
				exit.setText("退出");
				exit.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						netUploadError.dismiss();
						Activity_Control.this.finish();
					}
				});
				netUploadError.show();
			}

			if (msg.what == 6) {
//				autoUpLoadThead.interrupt();
				Activity_Control.this.finish();
			}
			if (msg.what == 7) {
				checkAuto = false;
			}
			if (msg.what == 8) {
				checkAuto = true;
			}
			if (msg.what == 11) {
				netErrorDialog = new Dialog(Activity_Control.this,
						R.style.MyDialogStyle);
				netErrorDialog.setContentView(R.layout.erro_dialog);
				TextView message = (TextView) netErrorDialog
						.findViewById(R.id.textView1);
				message.setText("網絡異常!");
				Button exit = (Button) netErrorDialog
						.findViewById(R.id.button1);
				exit.setText("返回");
				exit.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						netErrorDialog.dismiss();
					}
				});
				netErrorDialog.show();
			}
			if(msg.what == 12){
				totalMatch.setText(matchNum);
			}

		}
	};
	private Dialog userError = null;

	@SuppressLint("HandlerLeak")
	private class testHander extends Handler {
		@SuppressLint("HandlerLeak")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 0) {
				userError = new Dialog(Activity_Control.this,
						R.style.MyDialogStyle);
				userError.setContentView(R.layout.erro_dialog);
				ImageView image = (ImageView) userError
						.findViewById(R.id.imageView1);
				image.setImageResource(R.drawable.confirm_dialog_failweb);
				TextView message = (TextView) userError
						.findViewById(R.id.textView1);
				message.setText("退出提示,賬戶異地登陸!");
				Button exit = (Button) userError.findViewById(R.id.button1);
				exit.setText("退出");
				exit.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						userError.dismiss();
//						autoUpLoadThead.interrupt();
						Intent _intent = new Intent(Activity_Control.this,
								Login.class);
						Activity_Control.this.startActivity(_intent);
						Activity_Control.this.finish();
					}
				});
				userError.show();
			}
			// 更新
			if (msg.what == 1001) {
				update.showNoticeDialog();
			}
			if (msg.what == 500) {
				netError = new Dialog(Activity_Control.this,
						R.style.MyDialogStyle);
				netError.setContentView(R.layout.erro_dialog);
				TextView message = (TextView) netError
						.findViewById(R.id.textView1);
				message.setText("網絡連接故障!");
				Button exit = (Button) netError.findViewById(R.id.button1);
				exit.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						netError.dismiss();
						Intent _intent = new Intent(Activity_Control.this,
								Login.class);
						Activity_Control.this.startActivity(_intent);
						Activity_Control.this.finish();
					}
				});
				netError.show();
			}
		}
	}

	private UpdateManager update = new UpdateManager(this);
	private Dialog netError = null;

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		update.closeUpdate();
		if (userError != null) {
			userError.dismiss();
		}
		if (netError != null) {
			netError.dismiss();
		}
		if (netUploadError != null) {
			netUploadError.dismiss();
		}
		if (netErrorDialog != null) {
			netErrorDialog.dismiss();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		xianBetting = 0;
		heBetting = 0;
		zhuangBetting = 0;
		xianduiBetting = 0;
		zhuangduiBetting = 0;
		map.clear();
	}

}
